var util = getApp().util; 
var app = getApp()
var n = app.http.main_xiaoxiPost
var a;
var message_send = app.http.message_sendPost;  //接口 给主持发送未打卡通知
var save_punishment = app.http.save_punishmentPost;  //接口 保存对主持的惩罚方式
var infobydid = app.http.infobydidPost;  //接口 获取一条档期信息
var cftype = app.http.check_cf_typePost //判断惩罚方式
Page({
    data: {
      message_data:[],
      message_list:[],
      page:1,
      getData:false, //true 是没有更多数据
    },
    onLoad: function(t) {
      wx.hideShareMenu();//禁止分享
    },
  //加载更多
  _onScrollToLower: function () {
    if(this.data.getData){
      wx.showToast({
        title: "没有更多数据",
        icon: "none",
      })
    }else{
      this.xiaoxi_message(true);
    }
    //a.loadMoreComplete(!0);
  },
    onReady: function() {
      var that = this
      setTimeout(function () {
        // 延迟获取是为了解决真机上 windowHeight 值不对的问题
        wx.getSystemInfo({
          success: function (e) {
            that.setData({
              system: e
            });
          }
        });
      }, 500)
    },
    onShow: function() {
      var userInfo = wx.getStorageSync("userinfo");
       this.setData({
        message_list:[],
        userInfo,
        page:1
       })
        this.xiaoxi_message();

    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    xiaoxi_message: function(falg = false) {
        var t = this, o = wx.getStorageSync("userinfo").token, a = wx.getStorageSync("hall_info").id;
        n({
            token: o,
            hall_id: a,
            page:t.data.page
        },falg).then(function(n) {
           if (0 == n.code){
              if(n.data.length){
                var message_list = t.data.message_list
                var page = t.data.page + 1
                t.setData({
                  message_list:message_list.concat(n.data),
                  page:page
                })
              }else{
                wx.showToast({
                  title: "没有更多数据",
                  icon: "none",
                })
                t.setData({
                  getData:true
                })
              }

            }else{
             console.log("接口返回数据失败", n);
            }
        });
    },
    goXiugai: function(t) {
        var n = t.currentTarget.dataset.pid, o = t.currentTarget.dataset.signstatus;
        this.infobydid(n, function(t, n) {
            t ? console.log("档期info接口错误") : (wx.setStorageSync("update_status", {
                sign_status: o,
                type: 1
            }), wx.navigateTo({
                url: "../main_daka/main_daka"
            }));
        });
    },
    goBuka: function(t) {
        var n = t.currentTarget.dataset.pid, o = t.currentTarget.dataset.signstatus;
        this.infobydid(n, function(t, n) {
            t ? console.log("档期info接口错误") : (wx.setStorageSync("update_status", {
                sign_status: o,
                type: 3
            }), wx.navigateTo({
                url: "../main_daka/main_daka"
            }));
        });
    },
    infobydid: function(t, n) {
       if(!wx.getStorageSync("userinfo").token){
        wx.showToast({
          title: 'token不存在，请退出重新登录',
        })  
        return;
       }
        var a = wx.getStorageSync("userinfo").token;
        infobydid({
            token: a,
            did: t
        }).then(function(t) {
            0 == t.code ? (wx.setStorageSync("dangqiInfo", t.data), n(null, t)) : (console.log("接口返回数据失败", t), 
            n(t));
        }).catch(function(t) {
            n(t);
        });
    },
    //助补卡 1
    zhubuka(e) {
      if (e != undefined) {
        var clock_index = e.currentTarget.dataset.index
      } else {
        var clock_index = this.data.clock_index
      }
      var did = this.data.message_list[clock_index].pid
      this.infobydid(did, function (err, res) {
        if (!err) {
          wx.setStorageSync('update_status', { sign_status: 5, type: 3 });//管理员补卡
          wx.navigateTo({
            url: '../main_daka/main_daka',
          })
        } else {
          console.log("档期info接口错误");
        }
      })
    },
      //接口 判断惩罚方式是否可选
    cftypejiekou: function (name, cf_uid, info, callBack) {
      var that = this;
      let i;
      if (name.indexOf("周工资全减") != -1) {
        i = 1
      } else if (name.indexOf("日工资全减") != -1) {
        i = 2
      } else if (name.indexOf("周工资减半") != -1) {
        i = 3
      } else if (name.indexOf("日工资减半") != -1) {
        i = 4
      } else {
        i = 0
      }
      if (i && cf_uid) {
        //参数
        var token = wx.getStorageSync("userinfo").token
        var hall_id = wx.getStorageSync("hall_info").id
        var riqi = info.riqi
        cftype({
          token,
          hall_id,
          riqi,
          cf_uid,
          cf_type: i
        }, false).then(function (res) {
          if (res.code == 0) {
            if (res.check == 0) {
              callBack(true, "请选择其他惩罚方式", 0);
            } else {
              callBack(true, "成功", 1);
            }
          } else {
            callBack(false,"接口报错",res);
          }
        });
      } else {
        callBack(true,"成功" ,1);
      }
    },
    // 发送补卡通知1
  bukatongzhi(e) {
    this.setData({
      clock_index: e.currentTarget.dataset.index
    })
    this.send_message()
  },
  //接口 发送补卡通知2
  send_message() {
    var token = wx.getStorageSync('userinfo').token
    var hall_id = wx.getStorageSync('hall_info').id
    var clock_index = this.data.clock_index
    var dangqiid = this.data.message_list[clock_index].pid
    var message_id = this.data.message_list[clock_index].id
    // console.log(dangqiid, message_id);
    // return;
    message_send({
      token,
      hall_id,
      message_id,
      dangqiid
    }).then(re => {
      console.log(re);
      if (re.code == 0) {
        wx.showToast({
          title: '补卡信息已发送',
        })
        this.setData({
          message_list:[],
          page:1
        })
        this.xiaoxi_message(true)
      } else {
        console.log("接口返回数据失败", re);
      }
    })
  },
});